<?php
function dis_to_html($data,$dis){
		if(!$data||!$dis) echo '';
		$data_tmp = $data;
		$index = 0;
        $html='';
		foreach ($dis as $tpl) {
			$key = (int)key($tpl);
			$t = is_array($tpl[$key]) ? (string)$tpl[$key][0] : (string)$tpl[$key];
			$group = explode('|', $t);
			foreach ($group as $tp) {
				$g = explode(',', $tp);
				$tm = isset($g[1]) ? $g[1] : $g[0];
				$size = isset($g[1]) ? $g[0] : $key;
				if(!$tm) continue;
				$html.=TPL::module('youji_tpl/' . $tm, array(
					'box' => array_slice($data, $index, $size)
				),FALSE);
				$index+= $size;
			}
		}
		unset($data_tmp);
		return $html;
	}
/**
  *  图片量比较少时使用3和2组合 
  */
function parse_youji_ex($data){
    if(!$data) return array();
    $num_box = count($data);
    $tpl = V('-:yj_tpl_ex');
    //格式化数据
    $index='';
    foreach($data as $d){
        if($d['web_pic']['width']<$d['web_pic']['height']){
            $index.='1';
        }
        else{
            $index.='2';
        }
    }
    if($num_box<=3||($num_box==4&&isset($tpl[$index]))){
        return array(array($num_box=>$tpl[$index][rand(0,count($tpl[$index])-1)]));
    }
    else{
        $rst=array();
        if($num_box==4){
            $max_tmp=$max=2;
        }
        elseif($num_box<6){
            $max_tmp=$max=3;
        }
        else{
            $max_tmp=$max = V('-:yj_tpl_ex_max');
        }
        
        $index_begin=0;
        //5个的时候选择3 4个选择2
        $_rand=-1;
        while(($t=substr($index,$index_begin,$max_tmp--))&&($max_tmp>0)){
            if(isset($tpl[$t])){
                //连续2次的随机种子不一样 长度w为1b不计随机数
                if(($count=count($tpl[$t]))>1){
                    while($_rand==($rand=rand(0,$count-1))){ TRUE;}
                    $_rand=$rand;    
                }
                else{
                    $_rand=-1;
                    $rand=0;
                }
                
                array_push($rst,array(($max_tmp+1)=>$tpl[$t][$rand]));
                $index_begin+=($max_tmp+1);
                $left=strlen(substr($index,$index_begin));
                if($left==5){
                    $max_tmp=3;
                }
                elseif($left==4){
                    $max_tmp=2;
                }
                else{
                    $max_tmp=$max;
                }    
            }
        }
        return $rst;
    }
}

/**
  *  涉及的逻辑：
  *  1、标题特殊字体图片生成
  *  2、模板分配（简单s数字逻辑分配 根据内容分配，图片大小、文字多少）
  *  *  3、为整个m模块生成一张大图用于发微博 放在img_help方法体中处理
  *  @data 图片块数据结构
  *  @return 返回模板分配数组，顺序使用哪些模板，每个模板使用哪些box
  */
function parse_youji(&$data) {
    
    $num_box = count($data);
    $tpl = V('-:yj_tpl');
    $static_assign = array(
        7 => array(
            array(
                3,
                4
            )
        ) ,
        8 => array(
            array(
                4,
                4
            ) ,
            array(
                3,
                5
            )
        ) ,
        9 => array(
            array(
                4,
                5
            ) ,
            array(
                3,
                6
            )
        ) ,
        10 => array(
            array(
                5,
                5
            ) ,
            array(
                4,
                6
            )
        ) ,
        11 => array(
            array(
                4,
                4,
                3
            ) ,
            array(
                3,
                5,
                3
            )
        ) ,
        12 => array(
            array(
                5,
                4,
                3
            ) ,
            array(
                4,
                4,
                4
            )
        ) ,
        13 => array(
            array(
                5,
                4,
                4
            ) ,
            array(
                4,
                6,
                3
            )
        ) ,
        14 => array(
            array(
                5,
                6,
                3
            ) ,
            array(
                5,
                5,
                4
            )
        ) ,
        15 => array(
            array(
                5,
                5,
                5
            ) ,
            array(
                5,
                6,
                4
            )
        )
    );
    //YOUJI_TPL_FILENAME_FORMAT
    $index=0;
    $num_box_tmp = $num_box;
    $last_tpl = array();
    $tmp_data=array();//$data 的当前数据项
    while (($num_box_tmp-= ($choose = rand(3, 6))) > 9) {    
        $t=_parse_reorder($data,$tpl,$index,$choose);
        //reorder the data
        $last_tpl[] = array($choose=>$t[0]);
        
        $index+=$choose;
    }
    $t = array();
    $c = 0;
    $left=$num_box_tmp+$choose;
    if ($left >= 7) {
        $t = $static_assign[$left][rand(0, count($static_assign[$left]) - 1) ];
    }
    else {
        $t = array($left);
    }
    foreach ($t as $m) {
        $t=_parse_reorder($data,$tpl,$index,$m);
        $last_tpl[] = array($m=>$t[0]);
        $index+=$m;
    }
    return array($last_tpl,$data);
}
function _parse_reorder(&$data,$tpl,$index,$choose){
        $tmp_data=array_slice($data,$index,$choose);
        //choose the right template in same count group
        if(count($tpl[$choose])==1){
            $t=$tpl[$choose];
        }
        else{
            $c_data=array();
            foreach($tmp_data as $tmp){
                array_push($c_data,isset($tmp['width'])&&isset($tmp['height'])&&$tmp['width']>$tmp['height']);
            }
            sort($c_data);
            $t_index=0;$sum_max=1000000;
            foreach($tpl[$choose] as $tt){
                $sum=0;
                $t=0;
                $tt_tmp=$tt[1];
                sort($tt_tmp);
                while($t++<$choose){
                    $sum+=($c_data[$t-1]-$tt_tmp[$t-1])*($c_data[$t-1]-$tt_tmp[$t-1]);
                }
                if($sum<$sum_max){
                    $sum_max=$sum;
                    $t_index++;
                }
            }
            $t=$tpl[$choose][$t_index-1];
        }
        //choose the right template in same count group
        
        //reorder the data
        $ordered_data=array();
        
        //try to order
        
        foreach($t[1] as $tt){
            $fixed=0;
            $tmp_index=0;
            foreach($tmp_data as $tmp){
                if($tmp&&(isset($tmp['width'])&&isset($tmp['height'])&&$tmp['width']>$tmp['height']==$tt)){
                    array_push($ordered_data,$tmp);
                    $fixed=1;
                    $tmp_data[$tmp_index]=NULL;
                    break;
                }
                $tmp_index++;
            }
            if(!$fixed){
                array_push($ordered_data,NULL);
            }
            
        }
        //let it go
        $tmp_index=0;
        foreach($ordered_data as $od){
            if($od==NULL){
                $t_index=0;
                foreach($tmp_data as $tmp){
                    if($tmp!=NULL){
                        $ordered_data[$tmp_index]=$tmp;
                        $tmp_data[$t_index]=NULL;
                        break;
                    }
                    $t_index++;
                }        
            }
            $tmp_index++;
        }
        $tmp_index=-1;
        foreach($ordered_data as $d){
            $data[$index+(++$tmp_index)]=$d;
        }
        return $t;
    
}
function parse_content($data,$blance_load=TRUE){
    $txt=isset($data['text'])?$data['text']:NULL;
    //$txt='';
    $img=isset($data['web_pic'])?$data['web_pic']['url']:(isset($data['bmiddle_pic'])?$data['bmiddle_pic']:NULL);
    $width=isset($data['web_pic'])?$data['web_pic']['width']:(isset($data['width'])?$data['width']:NULL);
    $height=isset($data['web_pic'])?$data['web_pic']['height']:(isset($data['height'])?$data['height']:NULL);
    if($img){
        if($blance_load&&(!defined('ALBUM_BLANCE')||ALBUM_BLANCE)){
           echo sprintf('<div style="%s" class="img-box" rel="e:imgcm,t:%s,photo_id:'.$data['id'].'" rev="%s"  p="%s" dw="%s" dh="%s"><div class="loading"></div></div>%s','',F::escape($txt),$txt==NULL?0:1,$img,$width,$height,$txt?'<p>'.str_replace("\n","<br/>",F::escape($txt)).'</p>':'');    
        }
        else{
           echo sprintf('<div class="img-box" dw="%s" dh="%s"><img src="%s" /></div>',$width,$height,$img);  
        }
        
    }
    //if($txt){
    //    echo sprintf('<p>%s</p>',str_replace("\n",'<br/>',$txt));
    //}
}
//function parse_content($data){
//    $txt=isset($data['description'])?$data['description']:NULL;
//    $img=isset($data['bmiddle_pic'])?$data['bmiddle_pic']:NULL;
//    if($img){
//        echo sprintf('<div style="%s" class="img-box" rel="e:imgcm,t:%s,photo_id:'.$data['photo_id'].'" rev="%s"  p="%s" dw="%s" dh="%s"><div class="loading"></div></div>','',$txt,$txt==NULL?0:1,$img,isset($data['width'])?$data['width']:'',isset($data['height'])?$data['height']:'');
//    }
//    if($txt){
//        echo sprintf('<p>%s</p>',str_replace("\n",'<br/>',$txt));
//    }
//}
/*
function _parse_content($data,$dir='x',$box,$img_first=TRUE,$both=TRUE){
    $txt=isset($data['description'])?$data['description']:NULL;
    $img=isset($data['bmiddle_pic'])?$data['bmiddle_pic']:NULL;
    $img_html=$txt_html='';
    $s1=$box[1].'px';
    $s2=(($box[0]+$box[1])/2).'px';
    $s3=$box[0].'px';
    $font_size=14;
    
    if($both||$img){
        if(!isset($txt)||$txt==''){
            $width='100%';
            $height='100%';
        }
        else{
            $count_txt=mb_strlen($txt,'utf8');
            $step1=30;$step2=80;$step3=140;
            if($count_txt<=$step1){
                if($dir==='x'){
                    $width=$s1;
                    $height='100%';
                }
                else{
                    $height=$s1;
                    $width='100%';
                }
                $t=ceil(($step1-$count_txt)/$step1*3);
                $t=($t%2==0?$t:$t-1);
                $font_size+=$t;
            }
            elseif($count_txt<=$step2){
                if($dir==='x'){
                    $width=$s2;
                    $height='100%';
                }
                else{
                    $height=$s2;
                    $width='100%';  
                }
                //echo $count_txt;
                $t=ceil(($step2-$count_txt)/$step1*3);
                $t=($t%2==0?$t:$t-1);
                $font_size+=$t;
            }
            else{
                if($dir==='x'){
                    $width=$s3;
                    $height='100%';
                }
                else{
                    $height=$s3;
                    $width='100%';
                }
                $t=ceil(($step3-$count_txt)/$step1*3);
                $t=($t%2==0?$t:$t-1);
                $font_size+=$t;
            }
        }
        if($img){
            $img_html="<div style=\"width:".$width.";height:".$height.";\" class=\"img-box\"><img rel='e:imgcm,t:".
            (isset($txt)?$txt:'').
            "' src=\"".
            $img.
            "\"/></div>";
        }
        else{
            $img_html='';
        }
        
    }
    if($both||$txt){
        if(isset($data['is_html'])&&$data['is_html']){
            $txt_html=$txt;
        }
        elseif($txt){
            $txt_html='<p style="font-size:'.$font_size.'px">'.$txt.'</p>';
        }
        
    }
    if($img_first){
        echo $img_html;
        echo $txt_html;
    }
    else{
        echo $txt_html;
        echo $img_html;
    }
}
*/